<?php

namespace App\Livewire;

use App\Models\Actor;
use App\Models\Video;
use Illuminate\Support\Collection;
use Livewire\Component;
use Livewire\WithPagination;

class ActorShow extends Component
{
    use WithPagination;

    public Actor $actor;

    public Collection $collection;

    public int $page = 1;

    public bool $hasMorePages;

    public int $total = 0;

    public function mount(): void
    {
        $this->collection = Collection::make();

        $this->loadMore();
    }

    public function loadMore(): void
    {
        $collection = Video::query()
            ->whereRelation('actors', 'id', $this->actor->getKey())
            ->latest()
            ->paginate(perPage: 10, page: $this->page);

        $this->total = $collection->total();
        $this->collection->push(...$collection->items());

        if ($this->hasMorePages = $collection->hasMorePages()) {
            $this->page++;
        }
    }

    public function render()
    {
        return view('livewire.actor-show');
    }
}
